package com.time.hellotime.components.discovery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.time.hellotime.R;
import com.time.hellotime.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bing on 2017/11/14.
 * E-mail is tx16bing@163.com
 */

public class VolunteerActivity extends BaseActivity implements View.OnClickListener {

    public LinearLayout llParent;
    private TextView tvCollection, tvExecution, tvOver;
    private View oneLine, twoLine, threeLine;
    private ViewPager mPager;

    private List<Fragment> fragments = new ArrayList<>();
    private CollectionFrag collectionFrag;
    private ExecutionFrag executionFrag;
    private OverFrag overFrag;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, VolunteerActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_volunteer, "我的志愿");
        assignViews();
        initViewPager();
    }

    private void assignViews() {
        llParent = findView(R.id.ll_vol_parent);
        tvCollection = findView(R.id.tv_dis_one);
        tvExecution = findView(R.id.tv_dis_two);
        tvOver = findView(R.id.tv_dis_three);
        oneLine = findView(R.id.v_one_line);
        twoLine = findView(R.id.v_two_line);
        threeLine = findView(R.id.v_three_line);
        mPager = findView(R.id.vp_volunteer);

        tvCollection.setOnClickListener(this);
        tvExecution.setOnClickListener(this);
        tvOver.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.tv_dis_one:
                setTvColor(R.color.colorThirdPrimary, R.color.black_x, R.color.black_x);
                setLine(View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
                mPager.setCurrentItem(0, false);
                break;

            case R.id.tv_dis_two:
                setTvColor(R.color.black_x, R.color.colorThirdPrimary, R.color.black_x);
                setLine(View.INVISIBLE, View.VISIBLE, View.INVISIBLE);
                mPager.setCurrentItem(1, false);
                break;

            case R.id.tv_dis_three:
                setTvColor(R.color.black_x, R.color.black_x, R.color.colorThirdPrimary);
                setLine(View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
                mPager.setCurrentItem(2, false);
                break;

            default:
                break;

        }
    }

    private void setTvColor(int one, int two, int three) {
        tvCollection.setTextColor(ContextCompat.getColor(this, one));
        tvExecution.setTextColor(ContextCompat.getColor(this, two));
        tvOver.setTextColor(ContextCompat.getColor(this, three));
    }

    private void setLine(int one, int two, int three) {
        oneLine.setVisibility(one);
        twoLine.setVisibility(two);
        threeLine.setVisibility(three);
    }

    private void initViewPager() {
        collectionFrag = new CollectionFrag(application.getString(R.string.volunteer_flag));
        executionFrag = new ExecutionFrag(application.getString(R.string.volunteer_flag));
        overFrag = new OverFrag(application.getString(R.string.volunteer_flag));
        fragments.add(collectionFrag);
        fragments.add(executionFrag);
        fragments.add(overFrag);
//        mPager.setOffscreenPageLimit(2);
        mPager.setCurrentItem(0, false);
        mPager.setAdapter(new DisPagerAdapter());
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {

                    case 0:
                        setTvColor(R.color.colorThirdPrimary, R.color.black_x, R.color.black_x);
                        setLine(View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
                        break;

                    case 1:
                        setTvColor(R.color.black_x, R.color.colorThirdPrimary, R.color.black_x);
                        setLine(View.INVISIBLE, View.VISIBLE, View.INVISIBLE);
                        break;

                    case 2:
                        setTvColor(R.color.black_x, R.color.black_x, R.color.colorThirdPrimary);
                        setLine(View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
                        break;

                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private class DisPagerAdapter extends FragmentStatePagerAdapter {

        public DisPagerAdapter() {
            super(getSupportFragmentManager());
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

    }

}
